#include<iostream>

template <class T>
class unique_ptr
{
public:
    explicit unique_ptr(T* ptr) : _ptr(ptr) {}

    ~unique_ptr()
    {
        if (_ptr)
        {
            std::cout << "delete:" << _ptr << std::endl;
            delete _ptr;
        }
    }

    // 像指针一样使用
    T& operator*()
    {
        return *_ptr;
    }

    T* operator->()
    {
        return _ptr;
    }

    // 禁止拷贝构造和拷贝赋值
    unique_ptr(const unique_ptr<T>& sp) = delete;
    unique_ptr<T>& operator=(const unique_ptr<T>& sp) = delete;

    // 支持移动构造和移动赋值
    unique_ptr(unique_ptr<T>&& sp) : _ptr(sp._ptr)
    {
        std::cout << "移动构造" << std::endl;
        sp._ptr = nullptr;
    }

    unique_ptr<T>& operator=(unique_ptr<T>&& sp)
    {
        delete _ptr;
        _ptr = sp._ptr;
        sp._ptr = nullptr;
        return *this;
    }

private:
    T* _ptr;
};

int main()
{
    unique_ptr<int> auto_p1(new int(10));
    return 0;
}